<?php

namespace App\Controller;

use App\Annotations\LoginRequired;
use App\Entity\Address;
use App\Model\Table\AddressTable;
use App\UnifiedResponse;
use Cake\I18n\FrozenTime;
use Faker\Factory;
use Noodlehaus\Config;
use Wonfu\Core\Annotations\RequestMapping;
use DI\Annotation\Inject;
use Wonfu\Core\AppController;
use Predis\Client;

/**
 * Description of IndexCtrller
 *
 * @author vison.cao <visonforcoding@gmail.com>
 */
class IndexCtrller extends AppController
{

    /**
     * @Inject
     * @var Config
     */
    private $config;

    /**
     * @Inject()
     * @var Client
     */
    private $redis;

    /**
     * @LoginRequired
     * @RequestMapping(name="测试",path="/",method="GET")
     */
    public function test()
    {
        $addressTable = new AddressTable();
        /** @var Address $address */
        $address = $addressTable->get(1);
        $address->detail = '陆丰';
        $addressTable->getConnection()->begin();
        sleep(1);
        $addressTable->delete($address);
        $addressTable->getConnection()->commit();
    }

    /**
     * @RequestMapping(name="测试事务",path="/trans",method="POST")
     */
    public function trans(){
        $detail = $this->request->request->get('detail');
        $addressTable = new AddressTable();
        $addressTable->getConnection()->begin();
        /** @var Address $address */
        $address = $addressTable->get(1);
        $address->detail = $detail;
        $addressTable->getConnection()->commit();
        return new UnifiedResponse(0,'',$address);

    }

    /**
     * @RequestMapping(name="测试上传",path="/address/add",method="POST")
     */
    public function upload()
    {
        $faker = Factory::create('zh_CN');
        $address = new Address();
        $address->detail = $faker->address;
        $address->create_time = date('Y-m-d H:i:s');
        $address->modify_time = date('Y-m-d H:i:s');
        $addressTable = new AddressTable();
        $addressTable->save($address);

    }

}
